

package AddressBook.View;

import javax.swing.*;
import java.awt.event.ActionListener;

/**
 * The Class MyMenuBar provides and defines a custom bar for the main view
 */
public class MyMenuBar extends JMenuBar {

    /**
     * This field keeps a reference of the editMenu
     */
    private JMenu editMenu;

    /**
     * This field keeps a reference of the openMenuItem
     */
    private JMenuItem openMenuItem;

    /**
     * This field keeps a reference of the exitMenuItem
     */
    private JMenuItem exitMenuItem;

    /**
     * This field keeps a reference of the addItemEditMenu
     */
    private JMenuItem addItemEditMenu;

    /**
     * This field keeps a reference of the removeItemEditMenu
     */
    private JMenuItem removeItemEditMenu;

    /**
     * This field stores the name of the items of the menubar
     */
    private static final String[] ITEMS_MENUBAR = {"File", "Edit"};

    /**
     * This field stores the fields of the file menu
     */
    private static final String[] FILE_MENU_FIELDS = {"Open", "Save", "Exit"};

    /**
     * This field stores the fields of the edit menu
     */
    private static final String[] EDIT_MENU_FIELDS = {"Add Contact", "Remove Contact"};


    /**
     * This field stores a reference of the saveMenuItem
     */
    private JMenuItem saveMenuItem;


    /**
     * This constructor allows to create and initialize the menubar
     */
    public MyMenuBar() {
        super();

        JMenu fileMenu = new JMenu(ITEMS_MENUBAR[0]);
        editMenu = new JMenu(ITEMS_MENUBAR[1]);

        add(fileMenu);
        add(editMenu);

        editMenu.setVisible(false);

        openMenuItem = new JMenuItem(FILE_MENU_FIELDS[0]);
        saveMenuItem = new JMenuItem(FILE_MENU_FIELDS[1]);
        exitMenuItem = new JMenuItem((FILE_MENU_FIELDS[2]));

        saveMenuItem.setEnabled(false);

        fileMenu.add(openMenuItem);
        fileMenu.add(saveMenuItem);
        fileMenu.add(exitMenuItem);

        addItemEditMenu = new JMenuItem(EDIT_MENU_FIELDS[0]);
        removeItemEditMenu = new JMenuItem(EDIT_MENU_FIELDS[1]);

        editMenu.add(addItemEditMenu);
        editMenu.add(removeItemEditMenu);


    }

    /**
     * This method allow to the set the visibility of the edit and view menu
     *
     */
    public void updateVisibilityMenuBar() {
        saveMenuItem.setEnabled(true);
        editMenu.setVisible(true);
    }

    /**
     * This method allows to attach an action listener to the openMenu
     *
     * @param l The desired actionListener
     */
    public void addListenerOpenMenu(ActionListener l) {
        openMenuItem.addActionListener(l);
    }


    /**
     * This method allows to attach an action listener to the exitMenuItem
     *
     * @param l The desired actionListener
     */
    public void addListenerExitMenu(ActionListener l) {
        exitMenuItem.addActionListener(l);
    }

    /**
     * This method allows to attach an action listener to the editMenuItem
     *
     * @param l The desired actionListener
     */
    public void addListenerAddItemEditMenu(ActionListener l) {
        addItemEditMenu.addActionListener(l);
    }

    /**
     * This method allows to attach an action listener to the removeItemEditMenu
     *
     * @param l The desired actionListener
     */
    public void addListenerRemoveItemEditMenu(ActionListener l) {
        removeItemEditMenu.addActionListener(l);
    }

    /**
     * This method allows to attach an action listener to the saveFileMenuItem
     *
     * @param l The desired actionListener
     */
    public void addListenerSaveFileMenu(ActionListener l) {
        saveMenuItem.addActionListener(l);
    }

}
